﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace dropbox_screen_shooter
{
    public partial class formOptions : Form
    {
        public formOptions()
        {
            InitializeComponent();
            
            Options.Load();
            
            cbTypes.DataSource = Enum.GetValues(typeof(Types));
            cbTypes.DataBindings.Add(new Binding("SelectedItem", Options.Default, "Format", false, DataSourceUpdateMode.OnPropertyChanged));
            tbFolder.DataBindings.Add(new Binding("Text", Options.Default, "FolderString", false, DataSourceUpdateMode.OnValidation));
            tbDBID.DataBindings.Add(new Binding("Text", Options.Default, "DbID", false, DataSourceUpdateMode.OnPropertyChanged));
            chbAStart.DataBindings.Add(new Binding("Checked", Options.Default, "Autostart", false, DataSourceUpdateMode.OnPropertyChanged));
            chbPaste.DataBindings.Add(new Binding("Checked", Options.Default, "Paste", false, DataSourceUpdateMode.OnPropertyChanged));

            ScreenShooter.Screened += new ScreenShooter.ScreenEventHandler(ScreenShooter_Screened);

            KeyHook.hookID = KeyHook.SetHook(KeyHook.proc);
        }

        void ScreenShooter_Screened(ScreenShooter.ScreenEventArgs e)
        {
            niTrayIcon.BalloonTipTitle = "Screenshot has been made";
            if (Options.Default.Paste)
                niTrayIcon.BalloonTipText = string.Format("Saved file: {0} \nLink: {1}", e.FileName, e.PasteLink);
            else
                niTrayIcon.BalloonTipText = string.Format("Saved file: {0}", e.FileName);

            niTrayIcon.ShowBalloonTip(15);
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            Options.Save();
        }   

        private void niTrayIcon_DoubleClick(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            this.Visible = true;
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            WindowState = FormWindowState.Minimized;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeyHook.UnhookWindowsHookEx(KeyHook.hookID);
            Application.Exit();
        }

        private void bFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                Options.Default.FolderString = fbd.SelectedPath;
            }
        }
    }
}
    